package com.community.common.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.community.common.config.BizException;
import com.community.common.enums.CheckStateEnum;
import com.community.common.enums.IsOwnerEnum;
import com.community.common.mapper.OwnerHouseMapper;
import com.community.common.mapper.PublicUserMapper;
import com.community.common.pojo.domain.OwnerHouse;
import com.community.common.pojo.domain.PublicUser;
import com.community.common.pojo.response.CodeEnum;
import com.community.common.pojo.vo.OwnerCommunityVo;
import com.community.common.pojo.vo.RoleVo;
import com.community.common.util.CommonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author zhu
 * @Desc: 业主房屋关系
 */
@Service
public class OwnerHouseService extends ServiceImpl<OwnerHouseMapper, OwnerHouse> {

    @Autowired
    OwnerHouseMapper ownerHouseMapper;

    @Autowired
    PublicUserMapper publicUserMapper;

    public RoleVo checkByOpenId(String openId) {
        if (StringUtils.isBlank(openId)) {
            throw new BizException("openId不能为空");
        }
        LambdaQueryWrapper<PublicUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PublicUser::getOpenId, openId);
        PublicUser publicUser = publicUserMapper.selectOne(queryWrapper);
        if (publicUser == null || publicUser.getIdCard() == null || publicUser.getIdCard().length() < 10) {
            return RoleVo.builder().openId(openId).isOwner(IsOwnerEnum.N.getCode()).build();
        }

        LambdaQueryWrapper<OwnerHouse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OwnerHouse::getOpenId, openId).eq(OwnerHouse::getCheckState, CheckStateEnum.OK.getCode());
        // 业主
        if (ownerHouseMapper.selectCount(wrapper) > 0) {
            return RoleVo.builder().openId(openId).isCheck(CheckStateEnum.OK.getCode()).isOwner(IsOwnerEnum.Y.getCode()).build();
        }

        // 未审核
        wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OwnerHouse::getOpenId, openId).eq(OwnerHouse::getCheckState, CheckStateEnum.NOTHING.getCode());
        if (ownerHouseMapper.selectCount(wrapper) > 0) {
            return RoleVo.builder().openId(openId).isCheck(CheckStateEnum.NOTHING.getCode()).isOwner(IsOwnerEnum.Y.getCode()).build();
        }

        return RoleVo.builder().openId(openId).isOwner(IsOwnerEnum.N.getCode()).build();
    }

    public void add(OwnerHouse ownerHouse) {
        ownerHouseMapper.insert(ownerHouse.setId(CommonUtil.getIdStr()).setCreateTime(LocalDateTime.now()));
    }

    public void update(OwnerHouse ownerHouse) {
        if (Objects.isNull(ownerHouse) || StringUtils.isBlank(ownerHouse.getId())) {
            throw new BizException("主键不能为空");
        }
        ownerHouseMapper.updateById(ownerHouse);
    }

    public void delete(OwnerHouse ownerHouse) {
        if (Objects.isNull(ownerHouse) || StringUtils.isBlank(ownerHouse.getId())) {
            throw new BizException("主键不能为空");
        }
        OwnerHouse dbOwnerHouse = ownerHouseMapper.selectById(ownerHouse.getId());
        if (Objects.isNull(dbOwnerHouse) || !dbOwnerHouse.getOpenId().equals(ownerHouse.getOpenId())) {
            throw new BizException(CodeEnum.ABNORMAL_BELONGING.getMessage());
        }
        ownerHouseMapper.deleteById(ownerHouse.getId());
    }


    public List<OwnerCommunityVo> userCommunityList(String openId) {
        return ownerHouseMapper.selectCommunityByOpenId(openId);

    }

    public void submitCheck(String id) {
        OwnerHouse ownerHouse = ownerHouseMapper.selectById(id);
        if (StringUtils.equals(ownerHouse.getCheckState(), CheckStateEnum.FAIL.getCode())) {
            ownerHouse.setCheckState(CheckStateEnum.NOTHING.getCode());
            ownerHouseMapper.updateById(ownerHouse);
        }
    }

    public OwnerCommunityVo userCommunityDetail(String id) {
        return ownerHouseMapper.selectCommunityByOpenIdAndId(id);
    }
}
